package org.xxx.epms.gateway.security.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.ConfigAttribute;
import org.xxx.epms.gateway.dto.AdminPermissionDto;
import org.xxx.epms.gateway.feign.AdminFeign;
import org.xxx.epms.gateway.security.component.DynamicSecurityService;


import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Configuration
public class EpmsSequrityConfig {

    @Autowired
    private AdminFeign adminFeign;

    @Bean
    public DynamicSecurityService dynamicSecurityService() {
        return new DynamicSecurityService() {
            @Override
            public Map<String, ConfigAttribute> loadDataSource() {
                Map<String, ConfigAttribute> map = new ConcurrentHashMap<>();
                List<AdminPermissionDto> resourceList = adminFeign.getAdminPermissionList();
                for (AdminPermissionDto resource : resourceList) {
                    map.put(resource.getAdminPermissionUrl(), new org.springframework.security.access.SecurityConfig("[Admin-Permission]" + resource.getAdminPermissionName() + "(" +  resource.getAdminPermissionId() + ")" ));
                }
                return map;
            }
        };
    }
}
